package com.element.security.resource.config;

import com.element.security.resource.context.TokenContext;
import org.springframework.security.oauth2.provider.authentication.BearerTokenExtractor;

import javax.servlet.http.HttpServletRequest;

/**
 * 修改获取token方式
 *
 * @auther zhangwj
 * @date 2021/4/12 下午1:44
 */
public class AuthBearerTokenExtractor extends BearerTokenExtractor {

    private PermitAllUrlProperties permitAllUrlProperties;

    public AuthBearerTokenExtractor(PermitAllUrlProperties permitAllUrlProperties) {
        this.permitAllUrlProperties = permitAllUrlProperties;
    }

    // 修改获取token方式
    @Override
    protected String extractHeaderToken(HttpServletRequest request) {
        String url = request.getServletPath();
        if (null != permitAllUrlProperties && permitAllUrlProperties.hasMatcher(url)) {
            return null;
        }
        return TokenContext.getToken(request);
    }
}
